﻿using GalaSoft.MvvmLight;
using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// 系统设置
    /// </summary>
    public class SystemSetViewModel : ViewModelBase
    {
        public SystemSetViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                _RoomCount = RC.Current.RootData.温室集合.Count;
                this._数据记录间隔分钟数 = RC.Current.RootData.数据记录间隔分钟数;
                this._数据上传地址 = RC.Current.RootData.数据上传地址;
                this._启用数据上传 = RC.Current.RootData.启用数据上传;
                this._客户代码 = RC.Current.RootData.客户代码;
                this._忽略温室Id范围 = string.Join(",", RC.Current.RootData.IgnoreRoomIds.ToArray());
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        #region "RoomCount"

        private int _RoomCount = default(int);

        public int RoomCount
        {
            get
            {
                return _RoomCount;
            }
            set
            {
                if (_RoomCount == value)
                {
                    return;
                }

                var oldValue = _RoomCount;
                _RoomCount = value;

                RaisePropertyChanged("RoomCount");
            }
        }

        #endregion

        public ICommand SetRoomCountCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.SetRoomCount((byte)RoomCount);
                        _RoomRange.Clear();
                        ResetRoomRange();
                        Messenger.Default.Send<MessageBase>(new MessageBase(), "Reload温室实时数据集合");
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return RC.Current.RootData.温室集合.Count != RoomCount;
                        return true;
                    });
            }
        }


        #region "数据记录间隔分钟数"

        private int _数据记录间隔分钟数 = default(int);

        public int 数据记录间隔分钟数
        {
            get
            {
                return _数据记录间隔分钟数;
            }
            set
            {
                if (_数据记录间隔分钟数 == value)
                {
                    return;
                }
                _数据记录间隔分钟数 = value;
                RaisePropertyChanged("数据记录间隔分钟数");
            }
        }

        public ICommand 数据记录间隔分钟数Command
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.RootData.数据记录间隔分钟数 = 数据记录间隔分钟数;
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return RC.Current.RootData.数据记录间隔分钟数 != 数据记录间隔分钟数;
                        return true;
                    });
            }
        }

        #endregion



        #region "数据上传地址"

        private bool _启用数据上传 = default(bool);

        public bool 启用数据上传
        {
            get
            {
                return _启用数据上传;
            }
            set
            {
                if (_启用数据上传 == value)
                {
                    return;
                }
                _启用数据上传 = value;
                RaisePropertyChanged("启用数据上传");
            }
        }

        private string _数据上传地址 = default(string);

        public string 数据上传地址
        {
            get
            {
                return _数据上传地址;
            }
            set
            {
                if (_数据上传地址 == value)
                {
                    return;
                }
                _数据上传地址 = value;
                RaisePropertyChanged("数据上传地址");
            }
        }

        public ICommand 数据上传地址Command
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.RootData.数据上传地址 = 数据上传地址;
                        RC.Current.RootData.启用数据上传 = 启用数据上传;
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return RC.Current.RootData.数据上传地址 != 数据上传地址;
                        return true;
                    });
            }
        }

        #endregion


        #region "客户代码"

        private string _客户代码 = default(string);

        public string 客户代码
        {
            get
            {
                return _客户代码;
            }
            set
            {
                if (_客户代码 == value)
                {
                    return;
                }
                _客户代码 = value;
                RaisePropertyChanged("客户代码");
            }
        }

        public ICommand 客户代码Command
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.RootData.客户代码 = 客户代码;
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return RC.Current.RootData.客户代码 != 客户代码;
                        return true;
                    });
            }
        }

        #endregion


        #region "忽略温室Id范围"

        private string _忽略温室Id范围 = default(string);

        public string 忽略温室Id范围
        {
            get
            {
                return _忽略温室Id范围;
            }
            set
            {
                if (_忽略温室Id范围 == value)
                {
                    return;
                }
                _忽略温室Id范围 = value;
                RaisePropertyChanged("忽略温室Id范围");
            }
        }

        public ICommand 忽略温室Id范围Command
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.RootData.IgnoreRoomIds.Clear();
                        var ids = GetStringIds(忽略温室Id范围);
                        RC.Current.RootData.IgnoreRoomIds.AddRange(ids);
                        RC.Current.RootData.客户代码 = 客户代码;
                        RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return string.Join(",", RC.Current.RootData.IgnoreRoomIds.ToArray()) != 忽略温室Id范围;
                        return true;
                    });
            }
        }

        List<int> GetStringIds(string idsString)
        {
            if (!string.IsNullOrWhiteSpace(idsString))
            {
                return idsString.Split(',')
                                       .Where(p => !string.IsNullOrWhiteSpace(p))
                                       .Select(p => int.Parse(p))
                                       .Distinct()
                                       .OrderBy(p => p)
                                       .ToList();
            }
            return new List<int>();
        }

        #endregion

        #region validRoomId


        private ObservableCollection<VmCheckBox> _RoomRange = new ObservableCollection<VmCheckBox>();

        public ObservableCollection<VmCheckBox> RoomRange
        {
            get
            {
                if (_RoomRange.Count == 0)
                {
                    ResetRoomRange();
                }
                return _RoomRange;
            }
        }

        private void ResetRoomRange()
        {

            for (byte i = 0; i < this.RoomCount; i++)
            {
                _RoomRange.Add(
                    new VmCheckBox()
                    {
                        Tag = i + 1,
                        Content = GetRoomName((byte)(i + 1)),
                        ToolTip = "选中表示接收数据",
                        IsChecked = !RC.Current.RootData.IgnoreRoomIds.Contains(i + 1)
                    });
            }
        }

        string GetRoomName(byte id)
        {
            var room = RC.Current.RootData.温室集合.FirstOrDefault(p => p.Id == id);
            if (room == null)
            {
                return id.ToString() + "#";
            }
            return room.RoomName;
        }

        public ICommand RoomRangeCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        RC.Current.RootData.IgnoreRoomIds.Clear();
                        var ids = RoomRange.Where(p => !p.IsChecked).Select(p => (int)p.Tag).ToList();
                        RC.Current.RootData.IgnoreRoomIds.AddRange(ids);
                        RC.Current.SaveRootData();
                        Messenger.Default.Send<MessageBase>(new MessageBase(), "Reload温室实时数据集合");
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }

                        return true;
                    });
            }
        }

        #endregion

    }
}